<?php

namespace App\Http\Controllers\Admin;

use App\Models\Common\WebConfig;
use App\Models\Company\BuyResume;
use App\Models\Company\CompanyJob;
use App\Models\Company\CompanyLoginRecord;
use App\Models\Company\CompanyUser;
use App\Models\Personal\BrowseJob;
use App\Models\Personal\PersonalLoginRecord;
use App\Models\Personal\Resume;
use App\Models\Personal\ResumeSend;
use App\Models\XS\JobXS;
use App\Models\XS\ResumeXS;
use Carbon\Carbon;
use Style, DB;

class AdminController extends CommonController
{
    /**
     * 后台首页
     * @return mixed
     */
    public function index()
    {
        $threshold = 200;//触发警报阀值
        $data['untreated_job'] = DB::table('jobs')->where("available_at","<",time())->count();//未处理队列
        $data['warning_job'] = DB::table('failed_jobs')->count();//错误队列
        $xs = WebConfig::getConfig('xsOpen');
        //迅搜简历/职位偏差
        if ($xs == 1) {
            $jobXSTotal = @JobXS::getJobXSTotal();
            if ($jobXSTotal === false) {
                $data['xs_job'] = false;
            } else {
                $jobTotal = CompanyJob::where('job_status', 1)->count();
                $data['xs_job'] = $jobTotal - $jobXSTotal;
            }
            $resumeXSTotal = @ResumeXS::getResumeXSTotal();
            if ($resumeXSTotal === false) {
                $data['xs_resume'] = false;
            } else {
                $resumeTotal = Resume::where('resume_status', 1)->where('bkresume_status', 1)->count();
                $data['xs_resume'] = $resumeTotal - $resumeXSTotal;
            }
        }
        //今日登陆企业用户数和今日登陆个人用户数
        $startTime = Carbon::now()->format('Y-m-d 00:00:00');
        $endTime = Carbon::now()->format('Y-m-d 23:59:59');
        $data['company_login'] = CompanyLoginRecord::where('company_login_time' ,'>=' , $startTime)
            ->where('company_login_time', '<=', $endTime)
            ->count();
        $data['person_login'] = PersonalLoginRecord::where('personal_login_time' ,'>=' , $startTime)
            ->where('personal_login_time', '<=', $endTime)
            ->count();
        return Style::view('admin.home', compact('xs', 'threshold', 'data'));
    }

    /**
     * 获取当前时间
     * @return \Illuminate\Http\JsonResponse
     */
    public function getNowTime()
    {
        return response()->json(['status' => 0,'data' => round(time()*1000)]);
    }

    /**
     * 报表数据
     * @return \Illuminate\Http\JsonResponse
     */
    public function getReportData()
    {
        //图标报表数据
        for ($i = 7; $i > 0; $i--) {
            $timeArr[] = Carbon::now()->subDays($i)->format('Y-m-d');
            $timeOutput[] = Carbon::now()->subDays($i)->format('m-d');
        }
        foreach ($timeArr as $k => $v) {
            $startTime = $v . ' 00:00:00';
            $endTime = $v . ' 23:59:59';
            //近7日新增简历
            $reportData['resume_new'][] = Resume::where('regtime', '>=', $startTime)
                ->where('regtime', '<=', $endTime)
                ->count();
            //近7日简历投递
            $reportData['resume_send'][] = ResumeSend::where('created_at', '>=', $startTime)
                ->where('created_at', '<=', $endTime)
                ->count();
            //近7日下载简历
            $reportData['resume_dl'][] = BuyResume::where('created_at', '>=', $startTime)
                ->where('created_at', '<=', $endTime)
                ->count();
            //近7日新增注册企业
            $reportData['company_new'][] = CompanyUser::where('regtime', '>=', $startTime)
                ->where('regtime', '<=', $endTime)
                ->count();
            //近7日新增职位
            $reportData['job_new'][] = CompanyJob::where('addtime', '>=', $startTime)
                ->where('addtime', '<=', $endTime)
                ->count();
            //近7日被浏览职位
            $reportData['job_browse'][] = BrowseJob::where('created_at', '>=', $startTime)
                ->where('created_at', '<=', $endTime)
                ->count();
        }
        $reportName = [
            'resume_new' => '近7日新增简历',
            'resume_send' => '近7日简历投递',
            'resume_dl' => '近7日下载简历',
            'company_new' => '近7日新增注册企业',
            'job_new' => '近7日新增职位',
            'job_browse' => '近7日被浏览职位'
        ];
        $data['report_name'] = $reportName;
        $data['time_arr'] = $timeOutput;
        $data['report_data'] = $reportData;
        return response()->json(['status' => 0, 'data' => $data]);
    }
}